package com.sscaipiao.android.utils;

import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;

import android.util.Log;

public class StringUtil {
	
	 public static Integer[] getRandom(int maxNum, int resultNum) {
		  ArrayList<Integer> hs = new ArrayList<Integer>();
		  while (true) {
		   int a = (int) (Math.random() * (maxNum)+1);
		   if (a >= 1 && a <= maxNum && !hs.contains(a)) {
		    hs.add(a);
		   }
		   if (hs.size() == resultNum) {
		    break;
		   }
		  }

		  Integer[] a = new Integer[hs.size()];
		  for(int j  = 0 ; j < hs.size() ; j++) {
				Log.v("getRandom"+j, hs.get(j)+"");

			a[j] = hs.get(j);  
		  }

		 Arrays.sort(a);
//		  Object num2[] = new Object[a.length];
//
//		  for (int i = 0, j = 0; i < a.length; i++, j++) {
//		   num2[j] = a[i];
//		   System.out.println("产生随机数"+(i+1)+":"+num2[i]);
//		  }
		 
		  return a;
		 
		}

	public static boolean  isEmptyString(String inputStr){

		if(inputStr == null){
			return true;
		}else if (inputStr.equals("")|| inputStr.length()<=0){
			return true;
		}else {
			return false;
		}
		
	}
	
	public static boolean isNotEmptyString(String inputStr){

		if(inputStr == null){
			return false;
		}else if (inputStr.equals("")|| inputStr.length()<=0){
			return false;
		}else {
			return true;
		}
		
	}
	
	/** 
	 * 格式化数字为千分位显示； 
	 * @param 要格式化的数字； 
	 * @return 
	 */  
	public static String fmtMicrometer(String text)  
	{  
	    DecimalFormat df = null;  
	    if(text.indexOf(".") > 0)  
	    {  
	        if(text.length() - text.indexOf(".")-1 == 0)  
	        {  
	            df = new DecimalFormat("###,##0.");  
	        }else if(text.length() - text.indexOf(".")-1 == 1)  
	        {  
	            df = new DecimalFormat("###,##0.0");  
	        }else  
	        {  
	            df = new DecimalFormat("###,##0.00");  
	        }  
	    }else   
	    {  
	        df = new DecimalFormat("###,##0");  
	    }  
	    double number = 0.0;  
	    try {  
	         number = Double.parseDouble(text);  
	    } catch (Exception e) {  
	        number = 0.0;  
	    }  
	    return df.format(number);  
	} 
	
	
	public final static String str2MD5(String _str) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = _str.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
}
